﻿using DBUtil.FastEntity.Interface;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace DBUtil.FastEntity
{
    /// <summary>
    /// 实体操作工具
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IEntityUtil<T> where T : class, new()
    {
        /// <summary>
        /// 引用的DBAccess对象
        /// </summary>
        public DBAccess Db { get; }

        #region 增删改查入口

        #region 插入
        /// <summary>
        /// 从From中插入
        /// </summary>
        /// <param name="fromSelectClause">模型数据来源</param>
        /// <returns>插入的数量</returns>
        long InsertFromSelect(ISelectClause<T> fromSelectClause);

        /// <summary>
        /// 插入数据并获取生成的Id
        /// </summary>
        /// <returns></returns>
        int InsertAndGetId(T Model);

        /// <summary>
        /// 插入数据并获取生成的Id
        /// </summary>
        /// <returns></returns>
        long InsertAndGetIdLong(T Model);

        /// <summary>
        /// 插入数据并获取新的模型
        /// </summary>
        /// <param name="Model">模型</param>
        /// <param name="selectClause">已应用了:SelectByKey(1)</param>
        /// <returns></returns>
        T InsertAndGetModel(T Model, Action<ISelectClause<T>> selectClause = null);

        /// <summary>
        /// 插入数据并获取新的模型
        /// </summary>
        /// <param name="Models">模型集合</param>
        /// <param name="selectClause">已应用了:SelectByKey(1,2,3)</param>
        /// <returns></returns>
        List<T> InsertAndGetModels(List<T> Models, ISelectClause<T> selectClause = null);

        #endregion

        #region 删除

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <returns></returns>
        long Delete(params int[] key);

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        long Delete(params long[] key);

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        long Delete(params string[] key);

        /// <summary>
        /// 使用lamda表达式构造删除器
        /// </summary>
        /// <param name="expressions"></param>
        /// <returns></returns>
        long Delete(Expression<Func<T, bool>> expressions);
        #endregion

        #region 更新
        /// <summary>
        /// 根据dto对象更新表,dto对象必须拥有T中定义的主键
        /// </summary>
        /// <typeparam name="TDto"></typeparam>
        /// <param name="dto"></param>
        /// <returns></returns>
        long Update<TDto>(TDto dto) where TDto : class;

        /// <summary>
        /// 根据dto对象批量更新表,dto对象必须拥有T中定义的主键
        /// </summary>
        /// <typeparam name="TDto"></typeparam>
        /// <param name="dtos"></param>
        /// <returns></returns>
        long UpdateBatch<TDto>(List<TDto> dtos) where TDto : class;

        /// <summary>
        /// 根据dto对象和条件更新表，忽略dto中的主键
        /// </summary>
        /// <typeparam name="TDto"></typeparam>
        /// <param name="dto"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        long Update<TDto>(TDto dto, Expression<Func<T, bool>> expression) where TDto : class;

        /// <summary>
        /// 根据主键更新
        /// </summary>
        /// <param name="model"></param>
        /// <param name="selectClause"></param>
        /// <returns></returns>
        long Update(T model);

        /// <summary>
        /// 根据主键更新
        /// </summary>
        /// <param name="models"></param>
        /// <param name="selectClause"></param>
        /// <returns></returns>
        long UpdateBatch(List<T> models);
        #endregion

        #region 查询

        /// <summary>
        /// 简单查询构造器
        /// </summary>
        /// <returns></returns>
        ISelectClause<T> Select();

        /// <summary>
        /// 简单查询构造器
        /// </summary>
        /// <returns></returns>
        ISelectClause<T> SelectByKey(params int[] key);

        /// <summary>
        /// 简单查询构造器
        /// </summary>
        /// <returns></returns>
        ISelectClause<T> SelectByKey(params long[] key);

        /// <summary>
        /// 简单查询构造器
        /// </summary>
        /// <returns></returns>
        ISelectClause<T> SelectByKey(params string[] key);

        /// <summary>
        /// 简单查询构造器
        /// </summary>
        /// <returns></returns>
        ISelectClause<T> Select(Expression<Func<T, bool>> whereExpression);
        #endregion

        #endregion

    }
}
